package com.ruicar.afs.cloud.apply.contract.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.apply.common.entity.ApplyContractInfo;
import com.ruicar.afs.cloud.apply.common.entity.WitnessInfo;
import com.ruicar.afs.cloud.apply.common.feign.Apply2ChannelFeign;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConfig;
import com.ruicar.afs.cloud.apply.contract.mapper.ApplyContractInfoMapper;
import com.ruicar.afs.cloud.apply.contract.service.ApplyContractInfoService;
import com.ruicar.afs.cloud.apply.contract.vo.ApplyActiveContractVo;
import com.ruicar.afs.cloud.apply.contract.vo.ApplyAllAomVo;
import com.ruicar.afs.cloud.apply.contract.vo.ApplyContractVinVo;
import com.ruicar.afs.cloud.apply.contract.vo.ApplyStatisticsVo;
import com.ruicar.afs.cloud.bizcommon.business.condition.ApplyTotalCondition;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.security.vo.RoleVo;
import com.ruicar.afs.cloud.parameter.commom.enums.WhetherEnum;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Lee
 * @date: 2020/5/13 21:19
 * @description:
 */

@Service
@AllArgsConstructor
public class ApplyContractInfoServiceImpl extends ServiceImpl<ApplyContractInfoMapper, ApplyContractInfo> implements ApplyContractInfoService {
    protected Apply2ChannelFeign apply2ChannelFeign;
    protected ApplyConfig config;

    /**
     * 更新合同状态
     *
     * @param applyNo
     * @param applyStatus
     */
    public void updateContractStatus(String applyNo, String applyStatus) {
        ApplyContractInfo applyContractInfo = this.baseMapper.selectOne(Wrappers.<ApplyContractInfo>query().lambda()
                .eq(StringUtils.isNotEmpty(applyNo), ApplyContractInfo::getApplyNo, applyNo));
        applyContractInfo.setContractStatus(applyStatus);
        this.baseMapper.updateById(applyContractInfo);
    }

    /**
     * 获取见证人
     **/
    public JSONObject getWitnessInfo(WitnessInfo witnessInfo) {
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", config.getCaseClientId());
        requestHeader.put("clientSecret", config.getCaseClientSecret());
        return apply2ChannelFeign.getWitnessByChannelId(witnessInfo, requestHeader);
    }

    /**
     * 通过申请编号获取合同信息
     * @param applyNo
     * @return
     */
    public ApplyContractInfo getContractInfoByAppplyNo(String applyNo){

        ApplyContractInfo applyContractInfo = this.baseMapper.selectOne(Wrappers.<ApplyContractInfo>query().lambda().eq(ApplyContractInfo::getApplyNo,applyNo));
        return applyContractInfo;
    }

    /**
     * 通过合同模板获取合同信息
     * @param contractNo
     * @return
     */
    public ApplyContractInfo getContractInfoByContractNo(String contractNo){

        ApplyContractInfo applyContractInfo = this.baseMapper.selectOne(Wrappers.<ApplyContractInfo>query().lambda().eq(ApplyContractInfo::getContractNo,contractNo));
        return applyContractInfo;
    }

    /**
     * 获取合同申请信息
     * @param channelId,status,gpsStatus
     * @param status
     * @return
     */
    @Override
    public List<ApplyContractInfo> getDefaultList(String channelId,String status,String gpsStatus) {
        JSONArray jsonArray = SecurityUtils.getUser().getUserExtInfo().getJSONArray("deptIds");
        List<String> deptIds = null;
        List<RoleVo> roleList=null;
        String deptId="";
        if(jsonArray!=null){
            // 部门列表
            deptIds=jsonArray.toJavaList(String.class);
        }
        deptId=String.valueOf(SecurityUtils.getUser().getDeptId());
        JSONArray roleArray = SecurityUtils.getUser().getUserExtInfo().getJSONArray("roles");
        if(roleArray != null){
            roleList = roleArray.toJavaList(RoleVo.class);
        }
        List<ApplyContractInfo> applyOrderInfoList=this.baseMapper.getContractList(channelId,status,SecurityUtils.getUsername(),deptIds,roleList,deptId,gpsStatus);
        return applyOrderInfoList;
    }

    /**
     * 获取GPS二次派单信息
     * @param channelId,status
     * @param status
     * @return
     */
    @Override
    public List<ApplyContractInfo> getGpsAgainList(String channelId,String status) {
        List<ApplyContractInfo> applyOrderInfoList=this.baseMapper.getGpsAgainList(channelId,status);
        return applyOrderInfoList;
    }

    /**
     * 获取合同申请数量
     * @param applyTotalCondition
     * @return
     */
    @Override
    public List<ApplyContractInfo> getContractCount(ApplyTotalCondition applyTotalCondition) {
        List<ApplyContractInfo> applyOrderInfoList=this.baseMapper.getContractCount(applyTotalCondition);
        return applyOrderInfoList;
    }

    /**
     * 获取合同申请数量PC
     * @param applyTotalCondition
     * @return
     */
    @Override
    public List<ApplyStatisticsVo> getContractCountPc(ApplyTotalCondition applyTotalCondition) {
        List<ApplyStatisticsVo> applyOrderInfoList=this.baseMapper.getContractCountPc(applyTotalCondition);
        return applyOrderInfoList;
    }

    /**
     * 放款申请-更新重出标识
     * @param contractNo
     */
    public void updateReappearFlag(String contractNo){

        ApplyContractInfo applyContractInfo = this.getContractInfoByContractNo(contractNo);
        if(applyContractInfo!=null){
            applyContractInfo.setReappearFlag(WhetherEnum.No.getIndex());
            this.updateById(applyContractInfo);
        }
    }
    /**
     * 获取合同激活订单
     * */
    @Override
    public List<ApplyActiveContractVo> getContractInfoByCreditCode(String creditCode){
        List<ApplyActiveContractVo> applyContractInfoList=this.baseMapper.getContractInfoByCreditCode(creditCode);
        return applyContractInfoList;
    }
    /**
     * 获取合同激活订单
     * */
    @Override
    public List<ApplyAllAomVo> getContractAllAom(String creditCode, String criticalTime){
        List<ApplyAllAomVo> applyAllAomVoList=this.baseMapper.getContractAllAom(creditCode,criticalTime);
        return applyAllAomVoList;
    }
    @Override
    public List<ApplyContractInfo> getContractInfoByAom(String creditCode, String criticalTime){
        List<ApplyContractInfo> applyContractInfoList=this.baseMapper.getContractInfoByAom(creditCode,criticalTime);
        return applyContractInfoList;
    }
    @Override
    public List<ApplyContractVinVo> getContractInfoByVin(String vin){
        List<ApplyContractVinVo> applyContractInfoList=this.baseMapper.getContractInfoByVin(vin);
        return applyContractInfoList;
    }
}
